package com.data.biz.service.impl;

import com.data.biz.constant.AccTableResult;
import com.data.biz.constant.FunctionTypeEnum;
import com.data.biz.constant.TimeFormatConstant;
import com.data.biz.domain.*;
import com.data.biz.mapper.AccDataMapperEx;
import com.data.biz.mapper.AccDayMapper;
import com.data.biz.mapper.TableCountMapper;
import com.data.biz.mapper.WindPointMapper;
import com.data.biz.service.IBizAccService;
import com.data.biz.utils.ExcelFormatUtil;
import com.data.biz.vo.SensorVo.acc.AccGraphGroupVo;
import com.data.biz.vo.SensorVo.acc.AccGraphOneMapVo;
import com.data.biz.vo.SensorVo.dip.GraphGroupVo;
import com.data.biz.vo.SensorVo.dip.GraphOneMapVo;
import com.data.biz.vo.SensorVo.acc.AccTableVo;
import org.apache.commons.collections.CollectionUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;


@Service
public class BizAccservice implements IBizAccService {
    @Autowired
    private AccDataMapperEx accDataMapperEx;

    @Autowired
    private TableCountMapper countMapper;


    @Autowired
    private WindPointMapper windPointMapper;

    @Autowired
    private AccDayMapper accDayMapper;

    @Override
    public AccTableResult getAccTableDataByPoint(Integer pointId, String startDate, String endDate, Integer page, Integer pageSize) {
        AccTableResult result = new AccTableResult();

        List<AccTableVo> resultList = new ArrayList<>();
        WindPoint pointSearchEntity = new WindPoint();
        pointSearchEntity.setId(pointId);
        WindPoint point = windPointMapper.selectByPrimaryKey(pointId);
        String tableName = getTableByPoint(point);
        int totalCount = 0;
        List<AccData> list;
        if (page == null && pageSize == null) {
            ;
            list = accDataMapperEx.selectAccTable(tableName, startDate, endDate, null, null);
        } else {
            totalCount = accDataMapperEx.selectCount(tableName, startDate, endDate);
            list = accDataMapperEx.selectAccTable(tableName, startDate, endDate, (page - 1) * pageSize, pageSize);
        }
        for (AccData data : list) {
            AccTableVo vo = new AccTableVo();
            BeanUtils.copyProperties(data, vo);
            vo.setCreateTime(TimeFormatConstant.formatSec.format(data.getCreateTime()));
            resultList.add(vo);
        }
        result.setList(resultList);
        result.setTotalCount(totalCount);
        return result;
    }

    private String getTableByPoint(WindPoint point) {
        String tableName = point.getIp() + "_" + FunctionTypeEnum.getIndexById(point.getFunctionId()).toLowerCase(Locale.ROOT) + "_" + point.getCom();

        String name = tableName.replace('.', '_');
        return name;
    }


    @Override
    public GraphGroupVo getAccDataGroupGraph(Integer pointId, String startDate, String endDate) {
        GraphGroupVo result = new GraphGroupVo();
        List<List<GraphOneMapVo>> resultList = new ArrayList<>();
        List<GraphOneMapVo> oneMapVoListX = new ArrayList<>();
        List<GraphOneMapVo> oneMapVoListY = new ArrayList<>();
        List<GraphOneMapVo> oneMapVoListZ = new ArrayList<>();

        //对应倾角的XYZ

        WindPoint point = windPointMapper.selectByPrimaryKey(pointId);
        String tableName = getTableByPoint(point);

        List<AccData> list = accDataMapperEx.selectAccTable(tableName, startDate, endDate, null, null);
        LinkedHashMap<String, List<AccData>> map = new LinkedHashMap<>();
        for (AccData data : list) {
            String group = TimeFormatConstant.formatSec.format(data.getCreateTime());
            if (map.containsKey(group)) {
                List<AccData> mapList = map.get(group);
                mapList.add(data);
            } else {
                List<AccData> mapList = new ArrayList<>();
                mapList.add(data);
                map.put(group, mapList);
            }
        }


        for (String key : map.keySet()) {
            GraphOneMapVo voX = new GraphOneMapVo();
            GraphOneMapVo voY = new GraphOneMapVo();
            GraphOneMapVo voZ = new GraphOneMapVo();


            List<AccData> sourceList = map.get(key);

            List<String> dataListX = new ArrayList<>();
            List<String> dataListY = new ArrayList<>();
            List<String> dataListZ = new ArrayList<>();

            for (AccData data : sourceList) {
                    dataListX.add(data.getxMean().setScale(3, RoundingMode.HALF_UP).toString());
                    dataListX.add(data.getxMax().setScale(3, RoundingMode.HALF_UP).toString());
                    dataListX.add(data.getxMin().setScale(3, RoundingMode.HALF_UP).toString());
                    dataListX.add(data.getxEff().setScale(3, RoundingMode.HALF_UP).toString());
                    dataListY.add(data.getyMean().setScale(3, RoundingMode.HALF_UP).toString());
                    dataListY.add(data.getyMax().setScale(3, RoundingMode.HALF_UP).toString());
                    dataListY.add(data.getyMin().setScale(3, RoundingMode.HALF_UP).toString());
                    dataListY.add(data.getyEff().setScale(3, RoundingMode.HALF_UP).toString());
                    dataListZ.add(data.getzMean().setScale(3, RoundingMode.HALF_UP).toString());
                    dataListZ.add(data.getzMax().setScale(3, RoundingMode.HALF_UP).toString());
                    dataListZ.add(data.getzMin().setScale(3, RoundingMode.HALF_UP).toString());
                    dataListZ.add(data.getzEff().setScale(3, RoundingMode.HALF_UP).toString());
            }
            voX.setDataList(dataListX);
            voX.setTime(key);
            voY.setDataList(dataListY);
            voY.setTime(key);
            voZ.setDataList(dataListZ);
            voZ.setTime(key);
            oneMapVoListX.add(voX);
            oneMapVoListY.add(voY);
            oneMapVoListZ.add(voZ);
        }
        resultList.add(oneMapVoListX);
        resultList.add(oneMapVoListY);
        resultList.add(oneMapVoListZ);
        result.setList(resultList);
        return result;
    }


    @Override
    public GraphGroupVo getAccDataStatistic(Integer pointId, String startDate, String endDate) {
        GraphGroupVo result = new GraphGroupVo();
        List<List<GraphOneMapVo>> resultList = new ArrayList<>();
        List<GraphOneMapVo> oneMapVoListX = new ArrayList<>();
        List<GraphOneMapVo> oneMapVoListY = new ArrayList<>();
        List<GraphOneMapVo> oneMapVoListZ = new ArrayList<>();

        //对应倾角的XYZ

        WindPoint point = windPointMapper.selectByPrimaryKey(pointId);
        String tableName = getTableByPoint(point);

        List<AccData> list = accDataMapperEx.selectAccTable(tableName, startDate, endDate, null, null);
        LinkedHashMap<String, List<AccData>> map = new LinkedHashMap<>();
        for (AccData data : list) {
            String group = TimeFormatConstant.formatSec.format(data.getCreateTime());
            if (map.containsKey(group)) {
                List<AccData> mapList = map.get(group);
                mapList.add(data);
            } else {
                List<AccData> mapList = new ArrayList<>();
                mapList.add(data);
                map.put(group, mapList);
            }
        }


        for (String key : map.keySet()) {
            GraphOneMapVo voX = new GraphOneMapVo();
            GraphOneMapVo voY = new GraphOneMapVo();
            GraphOneMapVo voZ = new GraphOneMapVo();


            List<AccData> sourceList = map.get(key);

            List<String> dataListX = new ArrayList<>();
            List<String> dataListY = new ArrayList<>();
            List<String> dataListZ = new ArrayList<>();

            for (AccData data : sourceList) {
                dataListX.add(data.getxMean().toString());
                dataListX.add(data.getxMax().toString());
                dataListX.add(data.getxMin().toString());
                dataListX.add(data.getxEff().toString());
                dataListY.add(data.getyMean().toString());
                dataListY.add(data.getyMax().toString());
                dataListY.add(data.getyMin().toString());
                dataListY.add(data.getyEff().toString());
                dataListZ.add(data.getzMean().toString());
                dataListZ.add(data.getzMax().toString());
                dataListZ.add(data.getzMin().toString());
                dataListZ.add(data.getzEff().toString());
            }
            voX.setDataList(dataListX);
            voX.setTime(key);
            voY.setDataList(dataListY);
            voY.setTime(key);
            voZ.setDataList(dataListZ);
            voZ.setTime(key);
            oneMapVoListX.add(voX);
            oneMapVoListY.add(voY);
            oneMapVoListZ.add(voZ);
        }
        resultList.add(oneMapVoListX);
        resultList.add(oneMapVoListY);
        resultList.add(oneMapVoListZ);
        result.setList(resultList);
        return result;
    }


    @Override
    public AccGraphGroupVo getAccDataGroupGraphForPR(Integer pointId, String startDate, String endDate) {
        AccGraphGroupVo result = new AccGraphGroupVo();
        List<List<AccGraphOneMapVo>> resultList = new ArrayList<>();
        List<AccGraphOneMapVo> accOneMapVoListX = new ArrayList<>();
        List<AccGraphOneMapVo> accOneMapVoListY = new ArrayList<>();
        List<AccGraphOneMapVo> oneMapVoListZ = new ArrayList<>();

        //对应倾角的XYZ

        WindPoint point = windPointMapper.selectByPrimaryKey(pointId);
        String tableName = getTableByPoint(point);

        List<AccData> list = accDataMapperEx.selectAccTable(tableName, startDate, endDate, null, null);
        LinkedHashMap<String, List<AccData>> map = new LinkedHashMap<>();
        for (AccData data : list) {
            String group = TimeFormatConstant.formatSec.format(data.getCreateTime());
            if (map.containsKey(group)) {
                List<AccData> mapList = map.get(group);
                mapList.add(data);
            } else {
                List<AccData> mapList = new ArrayList<>();
                mapList.add(data);
                map.put(group, mapList);
            }
        }


        for (String key : map.keySet()) {
            AccGraphOneMapVo voX = new AccGraphOneMapVo();
            AccGraphOneMapVo voY = new AccGraphOneMapVo();
            AccGraphOneMapVo voZ = new AccGraphOneMapVo();


            List<AccData> sourceList = map.get(key);

            List<String> dataListXR = new ArrayList<>();
            List<String> dataListXP = new ArrayList<>();
            List<String> dataListYR = new ArrayList<>();
            List<String> dataListYP = new ArrayList<>();
            List<String> dataListZR = new ArrayList<>();
            List<String> dataListZP = new ArrayList<>();


            for (AccData data : sourceList) {
                    dataListXR.add(data.getxFre1().setScale(3, RoundingMode.HALF_UP).toString());
                    dataListXR.add(data.getxFre2().setScale(3, RoundingMode.HALF_UP).toString());
                    dataListXR.add(data.getxFre3().setScale(3, RoundingMode.HALF_UP).toString());
                    dataListXP.add(data.getxPow1().setScale(3, RoundingMode.HALF_UP).toString());
                    dataListXP.add(data.getxPow2().setScale(3, RoundingMode.HALF_UP).toString());
                    dataListXP.add(data.getxPow3().setScale(3, RoundingMode.HALF_UP).toString());
                    dataListYR.add(data.getyFre1().setScale(3, RoundingMode.HALF_UP).toString());
                    dataListYR.add(data.getyFre2().setScale(3, RoundingMode.HALF_UP).toString());
                    dataListYR.add(data.getyFre3().setScale(3, RoundingMode.HALF_UP).toString());
                    dataListYP.add(data.getyPow1().setScale(3, RoundingMode.HALF_UP).toString());
                    dataListYP.add(data.getyPow2().setScale(3, RoundingMode.HALF_UP).toString());
                    dataListYP.add(data.getyPow3().setScale(3, RoundingMode.HALF_UP).toString());
                    dataListZR.add(data.getzFre1().setScale(3, RoundingMode.HALF_UP).toString());
                    dataListZR.add(data.getzFre2().setScale(3, RoundingMode.HALF_UP).toString());
                    dataListZR.add(data.getzFre3().setScale(3, RoundingMode.HALF_UP).toString());
                    dataListZP.add(data.getzPow1().setScale(3, RoundingMode.HALF_UP).toString());
                    dataListZP.add(data.getzPow2().setScale(3, RoundingMode.HALF_UP).toString());
                    dataListZP.add(data.getzPow3().setScale(3, RoundingMode.HALF_UP).toString());
            }
            voX.setDataListForRate(dataListXR);
            voX.setDataListForPower(dataListXP);
            voX.setTime(key);

            voY.setDataListForRate(dataListYR);
            voY.setDataListForPower(dataListYP);
            voY.setTime(key);

            voZ.setDataListForRate(dataListZR);
            voZ.setDataListForPower(dataListZP);
            voZ.setTime(key);

            accOneMapVoListX.add(voX);
            accOneMapVoListY.add(voY);
            oneMapVoListZ.add(voZ);
        }
        resultList.add(accOneMapVoListX);
        resultList.add(accOneMapVoListY);
        resultList.add(oneMapVoListZ);
        result.setList(resultList);
        return result;
    }

    public void doSaveSingleAccData(AccData data, String deviceId) {
        WindPointExample example = new WindPointExample();
        WindPointExample.Criteria criteria = example.createCriteria();
        criteria.andFunctionTypeEqualTo("ACC");
        criteria.andIdEqualTo(2);
        WindPoint point = windPointMapper.selectByExample(example).get(0);
        List<AccData> saveList = new ArrayList<>();
        data.setPointId(point.getId());

        saveList.add(data);
        String tableName ="123_456_677_111_acc_com8";
        if (CollectionUtils.isNotEmpty(saveList)) {
            accDataMapperEx.batchInsert(saveList, tableName);
        }
        //
        setLastData(point, saveList);
        setDayData(point, saveList);
    }
    public void doSaveAcc(List<String> address, List<String> data) {
        String ip = address.get(0);
        String type = address.get(1);
        String com = address.get(2);
        String tableName = ip.replaceAll("\\.", "_") + "_" + type + "_" + com;
        WindPointExample example = new WindPointExample();
        WindPointExample.Criteria criteria = example.createCriteria();
        criteria.andIpEqualTo(ip);
        criteria.andComEqualTo(com);
        criteria.andFunctionIdEqualTo(FunctionTypeEnum.getIdByIndex(type));
        WindPoint point = windPointMapper.selectByExample(example).get(0);
        List<AccData> saveList = new ArrayList<>();
        for (String entity : data) {
            entity = entity.replaceAll("\\[", "").replaceAll("\\]", "");
            List<String> finalList = Arrays.asList(entity.split(","));
            BigDecimal test = new BigDecimal(999999);
            try {
                for (int i = 0; i < finalList.size(); i++) {
                    if (new BigDecimal(finalList.get(i)).compareTo(test) > 0) {
                        //最后一个是时间，会大于999999
                        if (i != finalList.size() - 1) {
                            throw new Exception();
                        }
                    }
                }
            } catch (Exception e) {
                continue;
            }

            setAccDataList(saveList, finalList, point);
        }
        if (CollectionUtils.isNotEmpty(saveList)) {
            accDataMapperEx.batchInsert(saveList, tableName);
        }

        setLastData(point, saveList);
        setDayData(point, saveList);

    }

    public void setDayData(WindPoint point, List<AccData> saveList) {
        Integer day = Integer.parseInt(TimeFormatConstant.formatToDay.format(saveList.get(0).getCreateTime()));
        AccDayExample example1 = new AccDayExample();
        AccDayExample.Criteria criteria1 = example1.createCriteria();
        criteria1.andPointIdEqualTo(point.getId());
        criteria1.andDayEqualTo(day);
        List<AccDay> list = accDayMapper.selectByExample(example1);
        AccDay dayData;
        if (CollectionUtils.isNotEmpty(list)) {
            dayData = list.get(0);
        } else {
            dayData = new AccDay();
        }
        for (AccData data : saveList) {
            BigDecimal xMax = data.getxMax();
            BigDecimal yMax = data.getyMax();
            BigDecimal zMax = data.getzMax();
            if (dayData.getxMax() == null) {
                dayData.setxMax(xMax);
            } else if (xMax.compareTo(dayData.getxMax()) > 0) {
                dayData.setxMax(xMax);
            }
            if (dayData.getyMax() == null) {
                dayData.setyMax(yMax);
            } else if (yMax.compareTo(dayData.getyMax()) > 0) {
                dayData.setyMax(yMax);
            }
            if (dayData.getzMax() == null) {
                dayData.setzMax(zMax);
            } else if (zMax.compareTo(dayData.getzMax()) > 0) {
                dayData.setzMax(zMax);
            }
        }
        if (CollectionUtils.isNotEmpty(list)) {
            dayData.setDay(day);
            accDayMapper.updateByPrimaryKeySelective(dayData);
        } else {
            dayData.setPointId(point.getId());
            dayData.setFanId(point.getFanId());
            dayData.setDay(day);
            dayData.setIsDelete((short) 0);
            accDayMapper.insert(dayData);
        }

    }


    public void setLastData(WindPoint point, List<AccData> saveList) {
        TableCountExample example1 = new TableCountExample();
        TableCountExample.Criteria criteria1 = example1.createCriteria();
        criteria1.andPointIdEqualTo(point.getId());
        List<TableCount> countlist = countMapper.selectByExample(example1);
        TableCount count = countlist.get(0);
        AccData data = saveList.get(saveList.size() - 1);
        count.setLastId(data.getId());
        count.setValue1(data.getxMean().toString());
        count.setValue2(data.getxMin().toString());
        count.setValue3(data.getxMax().toString());
        count.setValue4(data.getxEff().toString());
        count.setValue5(data.getxFre1().toString());
        count.setValue6(data.getxFre2().toString());
        count.setValue7(data.getxFre3().toString());
        count.setValue8(data.getxPow1().toString());
        count.setValue9(data.getxPow2().toString());
        count.setValue10(data.getxPow3().toString());
        ///
        count.setValue11(data.getyMean().toString());
        count.setValue12(data.getyMin().toString());
        count.setValue13(data.getyMax().toString());
        count.setValue14(data.getyEff().toString());
        count.setValue15(data.getyFre1().toString());
        count.setValue16(data.getyFre2().toString());
        count.setValue17(data.getyFre3().toString());
        count.setValue18(data.getyPow1().toString());
        count.setValue19(data.getyPow2().toString());
        count.setValue20(data.getyPow3().toString());
        //
        count.setValue21(data.getzMean().toString());
        count.setValue22(data.getzMin().toString());
        count.setValue23(data.getzMax().toString());
        count.setValue24(data.getzEff().toString());
        count.setValue25(data.getzFre1().toString());
        count.setValue26(data.getzFre2().toString());
        count.setValue27(data.getzFre3().toString());
        count.setValue28(data.getzPow1().toString());
        count.setValue29(data.getzPow2().toString());
        count.setValue30(data.getzPow3().toString());
        count.setCreateTime(data.getCreateTime());
        countMapper.updateByPrimaryKeySelective(count);
    }


    private static void setAccDataList(List<AccData> saveList, List<String> finalList, WindPoint point) {
        AccData accData = new AccData();
        accData.setPointId(point.getId());
        accData.setId(Long.valueOf(finalList.get(0).trim()));
        accData.setxMean(new BigDecimal(finalList.get(2).trim()));
        accData.setxMin(new BigDecimal(finalList.get(4).trim()));
        accData.setxMax(new BigDecimal(finalList.get(8).trim()));
        accData.setxEff(new BigDecimal(finalList.get(9).trim()));
        accData.setxFre1(new BigDecimal(finalList.get(10).trim()));
        accData.setxFre2(new BigDecimal(finalList.get(11).trim()));
        accData.setxFre3(new BigDecimal(finalList.get(12).trim()));
        accData.setxPow1(new BigDecimal(finalList.get(13).trim()));
        accData.setxPow2(new BigDecimal(finalList.get(14).trim()));
        accData.setxPow3(new BigDecimal(finalList.get(15).trim()));
        accData.setyMean(new BigDecimal(finalList.get(16).trim()));
        accData.setyMax(new BigDecimal(finalList.get(17).trim()));
        accData.setyMin(new BigDecimal(finalList.get(18).trim()));
        accData.setyEff(new BigDecimal(finalList.get(19).trim()));
        accData.setyFre1(new BigDecimal(finalList.get(24).trim()));
        accData.setyFre2(new BigDecimal(finalList.get(25).trim()));
        accData.setyFre3(new BigDecimal(finalList.get(26).trim()));
        accData.setyPow1(new BigDecimal(finalList.get(27).trim()));
        accData.setyPow2(new BigDecimal(finalList.get(28).trim()));
        accData.setyPow3(new BigDecimal(finalList.get(29).trim()));
        accData.setzMean(new BigDecimal(finalList.get(30).trim()));
        accData.setzMax(new BigDecimal(finalList.get(31).trim()));
        accData.setzMin(new BigDecimal(finalList.get(32).trim()));
        accData.setzEff(new BigDecimal(finalList.get(33).trim()));
        accData.setzFre1(new BigDecimal(finalList.get(38).trim()));
        accData.setzFre2(new BigDecimal(finalList.get(39).trim()));
        accData.setzFre3(new BigDecimal(finalList.get(40).trim()));
        accData.setzPow1(new BigDecimal(finalList.get(41).trim()));
        accData.setzPow2(new BigDecimal(finalList.get(42).trim()));
        accData.setzPow3(new BigDecimal(finalList.get(43).trim()));
        accData.settMean(new BigDecimal(finalList.get(44).trim()));
        accData.setIsDelete((short) 0);
        Long time = Long.parseLong(finalList.get(45).replace(".0", "")) * 1000l;
        Date date = new Date(time);
        accData.setCreateTime(date);
        saveList.add(accData);
    }


    public WindPoint getPointByEntity(WindPoint entity) {
        WindPointExample example = new WindPointExample();
        WindPointExample.Criteria criteria = example.createCriteria();
        if (entity.getFanId() != null) {
            criteria.andFanIdEqualTo(entity.getFanId());
        }
        if (entity.getFunctionId() != null) {
            criteria.andFunctionIdEqualTo(entity.getFunctionId());
        }
        if (entity.getId() != null) {
            criteria.andIdEqualTo(entity.getId());
        }
        criteria.andIsDeleteEqualTo((short) 0);
        List<WindPoint> list = windPointMapper.selectByExample(example);
        if (list != null && list.size() != 0) {
            return list.get(0);
        } else {
            return null;
        }
    }


    @Override
    public ResponseEntity<byte[]> exportAccExcel(HttpServletRequest request, HttpServletResponse response, Integer pointId, String startDate, String endDate) {
        try {
            List<AccTableVo> list = getAccTableDataByPoint(pointId, startDate, endDate, null, null).getList();
            downloadService baseFrontController = new downloadService();
            WindPoint point = windPointMapper.selectByPrimaryKey(pointId);
            return baseFrontController.buildResponseEntity(export((List<AccTableVo>) list), "测点" + point.getPointName() + "加速度数据.xls");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private InputStream export(List<AccTableVo> list) {
        ByteArrayOutputStream output = null;
        InputStream inputStream1 = null;
        SXSSFWorkbook wb = new SXSSFWorkbook(1000);// 保留1000条数据在内存中

        Sheet sheet = wb.createSheet();
        // 设置报表头样式
        CellStyle header = ExcelFormatUtil.headSytle(wb);// cell样式
        CellStyle content = ExcelFormatUtil.contentStyle(wb);// 报表体样式

        // 每一列字段名
        String[] strs = new String[]{
                "x加速度平均值", "x加速度最小值", "x加速度最大值", "x加速度有效值", "x加速度主振频率", "x加速度次振频率", "x加速度季振频率", "x加速度主振功率", "x加速度次振功率", "x加速度季振功率",
                "y加速度平均值", "y加速度最小值", "y加速度最大值", "y加速度有效值", "y加速度主振频率", "y加速度次振频率", "y加速度季振频率", "y加速度主振功率", "y加速度次振功率", "y加速度季振功率",
                "z加速度平均值", "z加速度最小值", "z加速度最大值", "z加速度有效值", "z加速度主振频率", "z加速度次振频率", "z加速度季振频率", "z加速度主振功率", "z加速度次振功率", "z加速度季振功率",
                "采集时间"};

        // 字段名所在表格的宽度
        int[] ints = new int[]{5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000};

        // 设置表头样式
        ExcelFormatUtil.initTitleEX(sheet, header, strs, ints);

        if (list != null && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                AccTableVo vo = list.get(i);
                Row row = sheet.createRow(i + 1);
                int j = 0;

                Cell cell = row.createCell(j++);
                cell.setCellValue(vo.getXMean().toString()); //
                cell.setCellStyle(content);


                cell = row.createCell(j++);
                cell.setCellValue(vo.getXMin().toString()); //
                cell.setCellStyle(content);

                cell = row.createCell(j++);
                cell.setCellValue(vo.getXMax().toString()); //
                cell.setCellStyle(content);


                cell = row.createCell(j++);
                cell.setCellValue(vo.getXEff().toString()); //
                cell.setCellStyle(content);


                cell = row.createCell(j++);
                cell.setCellValue(vo.getXFre1().toString()); //
                cell.setCellStyle(content);


                cell = row.createCell(j++);
                cell.setCellValue(vo.getXFre2().toString()); //
                cell.setCellStyle(content);

                cell = row.createCell(j++);
                cell.setCellValue(vo.getXFre3().toString()); //
                cell.setCellStyle(content);

                cell = row.createCell(j++);
                cell.setCellValue(vo.getXPow1().toString()); //
                cell.setCellStyle(content);


                cell = row.createCell(j++);
                cell.setCellValue(vo.getXPow2().toString()); //
                cell.setCellStyle(content);

                cell = row.createCell(j++);
                cell.setCellValue(vo.getXPow3().toString()); //
                cell.setCellStyle(content);

                /////////////////////Y方向
                cell = row.createCell(j++);
                cell.setCellValue(vo.getYMean().toString()); //
                cell.setCellStyle(content);


                cell = row.createCell(j++);
                cell.setCellValue(vo.getYMin().toString()); //
                cell.setCellStyle(content);

                cell = row.createCell(j++);
                cell.setCellValue(vo.getYMax().toString()); //
                cell.setCellStyle(content);


                cell = row.createCell(j++);
                cell.setCellValue(vo.getYEff().toString()); //
                cell.setCellStyle(content);


                cell = row.createCell(j++);
                cell.setCellValue(vo.getYFre1().toString()); //
                cell.setCellStyle(content);


                cell = row.createCell(j++);
                cell.setCellValue(vo.getYFre2().toString()); //
                cell.setCellStyle(content);

                cell = row.createCell(j++);
                cell.setCellValue(vo.getYFre3().toString()); //
                cell.setCellStyle(content);

                cell = row.createCell(j++);
                cell.setCellValue(vo.getYPow1().toString()); //
                cell.setCellStyle(content);


                cell = row.createCell(j++);
                cell.setCellValue(vo.getYPow2().toString()); //
                cell.setCellStyle(content);

                cell = row.createCell(j++);
                cell.setCellValue(vo.getYPow3().toString()); //
                cell.setCellStyle(content);

                ////////////////////z方向
                cell = row.createCell(j++);
                cell.setCellValue(vo.getZMean().toString()); //
                cell.setCellStyle(content);


                cell = row.createCell(j++);
                cell.setCellValue(vo.getZMin().toString()); //
                cell.setCellStyle(content);

                cell = row.createCell(j++);
                cell.setCellValue(vo.getZMax().toString()); //
                cell.setCellStyle(content);


                cell = row.createCell(j++);
                cell.setCellValue(vo.getZEff().toString()); //
                cell.setCellStyle(content);


                cell = row.createCell(j++);
                cell.setCellValue(vo.getZFre1().toString()); //
                cell.setCellStyle(content);


                cell = row.createCell(j++);
                cell.setCellValue(vo.getZFre2().toString()); //
                cell.setCellStyle(content);

                cell = row.createCell(j++);
                cell.setCellValue(vo.getZFre3().toString()); //
                cell.setCellStyle(content);

                cell = row.createCell(j++);
                cell.setCellValue(vo.getZPow1().toString()); //
                cell.setCellStyle(content);


                cell = row.createCell(j++);
                cell.setCellValue(vo.getZPow2().toString()); //
                cell.setCellStyle(content);

                cell = row.createCell(j++);
                cell.setCellValue(vo.getZPow3().toString()); //
                cell.setCellStyle(content);

                cell = row.createCell(j++);
                cell.setCellValue(vo.getCreateTime()); // 采集时间
                cell.setCellStyle(content);
            }
        }
        try {
            output = new ByteArrayOutputStream();
            wb.write(output);
            inputStream1 = new ByteArrayInputStream(output.toByteArray());
            output.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (output != null) {
                    output.close();
                    if (inputStream1 != null)
                        inputStream1.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return inputStream1;
    }


}
